// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { battle_actionType } from "./common/common_enum";
import GameConfig from "./config/GameConfig";
import EventManager from "./manager/EventManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class main extends cc.Component {

    @property(cc.Camera)
    m_Camera: cc.Camera = null;
    @property(cc.Node)
    ui_list: cc.Node = null;
    @property(cc.Node) 
    player: cc.Node = null;
    @property(cc.Node) 
    boss: cc.Node = null;

    @property(cc.Node)
    back: cc.Node = null;

    

    onLoad () {
        this.uiInit();
        this.gameInit();
        EventManager.Inst.on("inBattle",this,this.inBattle)
    }

    uiInit(){
        let camSlider:cc.Slider = this.ui_list.getChildByName("camSlider").getComponent(cc.Slider)
        camSlider.node.on("slide",()=>{
            this.m_Camera.zoomRatio = 1 + camSlider.progress
            let lab = camSlider.node.getChildByName("lab_v").getComponent(cc.Label)
            lab.string = "缩放："+this.m_Camera.zoomRatio
        },this)

        let camSliderY:cc.Slider = this.ui_list.getChildByName("camSliderY").getComponent(cc.Slider)
        camSliderY.node.on("slide",()=>{
            this.m_Camera.node.y = -camSliderY.progress * 300
            let lab = camSliderY.node.getChildByName("lab_v").getComponent(cc.Label)
            lab.string = "y:"+this.m_Camera.node.y
            //console.log("缩放：",this.m_Camera.node.y)
        },this)
        this.actionMenu();

    }

    gameInit(){
        //初始化 物理碰撞系统
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        
        let physicsManager = cc.director.getPhysicsManager();
        physicsManager.enabled = true;
        physicsManager.enabledAccumulator = true; //让物理引擎 开启固定频率刷新

        // physicsManager.debugDrawFlags =
        // cc.PhysicsManager.DrawBits.e_aabbBit |
        // cc.PhysicsManager.DrawBits.e_jointBit |
        // cc.PhysicsManager.DrawBits.e_shapeBit
        // ;

        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        //manager.enabledDebugDraw = true;
    }

    inBattle(){
        //进入战斗
        console.log("进入战斗!!!")
        GameConfig.isBattle = true;
        cc.tween(this.m_Camera).to(0.2,{zoomRatio:1.6}).start()
        cc.tween(this.back).to(0.2,{scale:0.45}).start()
        cc.tween(this.back).by(0.2,{y:-160}).start()
        cc.tween(this.m_Camera.node).by(0.2,{x:350,y:-300}).start()
        let battleUI = this.ui_list.getChildByName("battleUI")
        battleUI.getComponent(cc.Animation).play();

        this.boss.active = true;
        cc.tween(this.boss).to(0.2,{opacity:255}).start();

        this.player.getComponent("player").inBattle(this.boss);
        
    }

    outBattle(){
        //退出战斗
        console.log("退出战斗!!!")
        GameConfig.isBattle = false;
        cc.tween(this.m_Camera.node).by(0.2,{x:-480,y:300}).start()
        //cc.tween(this.m_Camera.node).by(0.2,{x:-480,y:0}).start()
        //this.m_Camera.node.x -= 480
    }

    actionMenu(){
        //行动菜单
        let battleInfo = this.ui_list.getChildByName("battleInfo")
        let lab_desc:cc.Label = battleInfo.getChildByName("ScrollView").getChildByName("view").getChildByName("content").getChildByName("lab_desc").getComponent(cc.Label)
        lab_desc.string +="\n"
        let battleUI = this.ui_list.getChildByName("battleUI")
        let btn_attack1 = battleUI.getChildByName("btn_attack1")
        let btn_attack2 = battleUI.getChildByName("btn_attack2")
        let btn_attack3 = battleUI.getChildByName("btn_attack3")

        btn_attack1.on("click",()=>{EventManager.Inst.emit("addAction-player",battle_actionType.red)
        lab_desc.string +="行动【腿部】->"   },this)
        btn_attack2.on("click",()=>{EventManager.Inst.emit("addAction-player",battle_actionType.green)
        lab_desc.string +="行动【手部】->" },this)
        btn_attack3.on("click",()=>{EventManager.Inst.emit("addAction-player",battle_actionType.blue)
        lab_desc.string +="行动【献祭】->"},this)

        let btn_do = battleUI.getChildByName("btn_do")

        btn_do.on("click",()=>{
            //开始执行操作
            lab_desc.string +="执行当前行动\n"
            EventManager.Inst.emit("removeAction-player")
        },this)


        let btn_repeat = battleUI.getChildByName("btn_repeat")
        btn_repeat.on("click",()=>{
            //开始执行操作
            lab_desc.string +="================回放全部行动================\n\n"
        },this)

    }



    start () {

    }

    // update (dt) {}
}
